package cn.xz.hfsk.dialog

import android.view.Gravity
import android.widget.TextView
import cn.xz.hfsk.R
import cn.xz.hfsk.base.BaseDialogRxFragment
import cn.xz.hfsk.bean.DeviceList
import cn.xz.hfsk.net.ControlDevice
import cn.xz.hfsk.util.gone
import cn.xz.hfsk.util.visible
import cn.xz.hfsk.widgets.AutoScaleTextView

/**
 * 设备列表操作弹窗
 * */
class DeviceClickDialog: BaseDialogRxFragment() {

    //关闭弹窗按钮
    private var tvFinish: TextView? = null
    //设备名称
    private var tvDeviceName: AutoScaleTextView? = null
    //设备IP信息
    private var tvDeviceIp: TextView? = null
    //开启按钮
    private var tvStart: TextView? = null
    //关闭按钮
    private var tvStop: TextView? = null
    //重启按钮
    private var tvRestart: TextView? = null
    //设备信息
    private var mBean: DeviceList? = null

    companion object {
        fun newInstance(
            data: DeviceList, //是否有重启按钮
        ): DeviceClickDialog {
            val dialog = DeviceClickDialog()
            dialog.mBean = data
            return dialog
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.dialog_device_click
    }

    override fun configDialog() {
        this.setDialogSize(gravity = Gravity.CENTER,width = 400)
    }

    override fun initViews() {

        dialog?.let {
            tvFinish = it.findViewById(R.id.tv_finish)
            tvDeviceName = it.findViewById(R.id.tv_device_name)
            tvDeviceIp = it.findViewById(R.id.tv_device_ip)
            tvStart = it.findViewById(R.id.tv_start)
            tvStop = it.findViewById(R.id.tv_stop)
            tvRestart = it.findViewById(R.id.tv_restart)
        }

        //点击外部不隐藏
        dialog?.setCancelable(false)
        dialog?.setCanceledOnTouchOutside(false)
        //视图
        initUI()
        //点击事件
        initEvent()
    }

    private fun initUI(){
        if (mBean != null){
            //设备名称
            tvDeviceName?.text = mBean!!.name.toString()
            //IP信息
            if (mBean!!.ip != null && mBean!!.ip!!.isNotEmpty()){
                if (mBean!!.deviceType == 1){
                    tvDeviceIp?.text = "${mBean!!.switchIP} : ${mBean!!.switchPort}"
                }else{
                    tvDeviceIp?.text = "${mBean!!.ip} : ${mBean!!.port}"
                }
            }
            //是否显示重启
            if (mBean!!.deviceType == 2 || mBean!!.deviceType == 3 || mBean!!.deviceType == 9
                || mBean!!.deviceType == 11 || mBean!!.deviceType == 13){
                tvRestart?.visible()
            }else{
                tvRestart?.gone()
            }
        }
    }

    private fun initEvent(){
        //关闭弹窗
        tvFinish?.setOnClickListener {
            dismiss()
        }
        //开启
        tvStart?.setOnClickListener {
            if (mBean != null){
                ControlDevice.openDevice(mBean!!)
            }
        }
        //关闭
        tvStop?.setOnClickListener {
            if (mBean != null){
                ControlDevice.closeDevice(mBean!!)
            }
        }
        //重启
        tvRestart?.setOnClickListener {
            if (mBean != null && mBean!!.ip != null && mBean!!.ip!!.isNotEmpty()){
                ControlDevice.restartPC(mBean!!.ip!!)
            }
        }
    }

}